package com.org.csu.controller;

import com.org.csu.common.CommonResponse;
import com.org.csu.common.HotTrendResponse;
import com.org.csu.dto.HotwordDTO;
import com.org.csu.dto.KeywordDTO;
import com.org.csu.entity.CompKey;
import com.org.csu.entity.Word;
import com.org.csu.service.HotwordService;
import com.org.csu.service.KeywordRecordService;
import com.org.csu.service.KeywordService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Yigonglily
 * @version V1.0
 * Copyright (c) 2024, t8669@qq.com All Rights Reserved.
 * @ProjectName:e-commerce-back
 * @Title: KeywordController
 * @Package com.org.csu.controller
 * @Description: 关键词controller
 * @date 2024/12/15 10:41
 */

@RestController
@CrossOrigin(origins = "http://localhost:5173")
public class KeywordController {
    private final KeywordService keywordService;
    private final HotwordService hotwordService;
    private final KeywordRecordService keywordRecordService;

    // 构造函数注入KeywordService
    public KeywordController(KeywordService keywordService, HotwordService hotwordService,KeywordRecordService keywordRecordService) {
        this.keywordService = keywordService;
        this.hotwordService = hotwordService;
        this.keywordRecordService = keywordRecordService;
    }
    // 构造函数注入HotwordService

    // 实现GET /keywordInfo接口
    @GetMapping("/keywordInfo")
    public CommonResponse<KeywordDTO> getKeywordInfo(@RequestParam(value = "userId", required = false) String userId,
                                                     @RequestParam(value = "keyword", required = false) String keyword) {
        // 调用service层方法获取关键词信息
        KeywordDTO keywordInfo = keywordService.getKeywordInfo(keyword);
        // 打印日志，检查参数
        System.out.println("Received userId: " + userId + ", keyword: " + keyword);
        //搜索记录存入数据库
        handleSearchRequest(keyword, Integer.parseInt(userId));
        if(keywordInfo==null){
            return CommonResponse.createForError("今日份关键词不存在");
        }
        // 检查是否需要根据userId进一步处理数据，如果userId是必需的，可能需要在这里添加逻辑

        // 返回响应
        return CommonResponse.createForSuccess(keywordInfo);
    }
    // 实现GET /keyword/rate接口
    @GetMapping("/keyword/rate")
    public CommonResponse<Object> getKeywordRate(@RequestParam(value = "userId", required = false) String userId,
                                                 @RequestParam(value = "keyword", required = false) String keyword) {
        // 调用service层方法获取关键词的SEO难度
        Integer rate = keywordService.getKeywordRate(keyword);
        if(rate==null){
            return CommonResponse.createForError("今日份关键词不存在");
        }
        // 检查是否需要根据userId进一步处理数据，如果userId是必需的，可能需要在这里添加逻辑

        // 返回响应
        return CommonResponse.createForSuccess(rate);
    }
    // 实现GET /keyword/mid接口
    @GetMapping("/keyword/mid")
    public CommonResponse<List<KeywordDTO>> getKeywordMidWords(@RequestParam(value = "userId", required = false) String userId,
                                                               @RequestParam(value = "keyword", required = false) String keyword) {
        // 调用service层方法获取关键词的中介词和长尾词
        List<KeywordDTO> midWords = keywordService.getLongTailWords(keyword);
        if(midWords==null){
            return CommonResponse.createForError("今日份关键词长尾词不存在");
        }
        // 检查是否需要根据userId进一步处理数据，如果userId是必需的，可能需要在这里添加逻辑

        // 返回响应
        return CommonResponse.createForSuccess(midWords);
    }
    // 实现GET /keyword/comp接口
    @GetMapping("/keyword/comp")
    public CommonResponse<List<CompKey>> getCompetitiveWords(@RequestParam(value = "userId", required = false) String userId,
                                                             @RequestParam(value = "keyword", required = false) String keyword) {
        // 调用service层方法获取关键词的竞争词
        List<CompKey> competitiveWords = keywordService.getCompetitiveWords(keyword);
        System.out.println(competitiveWords);
        if(competitiveWords==null){
            return CommonResponse.createForError("今日份关键词不存在");
        }
        // 检查是否需要根据userId进一步处理数据，如果userId是必需的，可能需要在这里添加逻辑

        // 返回响应
        return CommonResponse.createForSuccess(competitiveWords);
    }
    // 实现GET /trend接口
    @GetMapping("/trend")
    public CommonResponse<List<Word>> getTrend(@RequestParam(value = "keyword", required = false) String keyword) {
        // 调用service层方法获取关键词的近七天搜索趋势
        List<Word> trendData = keywordService.getKeywordTrend(keyword);
        if(trendData==null){
            return CommonResponse.createForError("今日份关键词不存在");
        }
        // 返回响应
        return CommonResponse.createForSuccess(trendData);
    }
    // 实现GET /trend/hot接口
    @GetMapping("/trend/hot")
    public CommonResponse<HotTrendResponse> getHotTrends() {
        // 调用service层方法获取当日当周当月的热门搜索
        List<List<HotwordDTO>>hotTrends = hotwordService.getHotword();

        // 将Object转换为HotTrendsResponse对象
        HotTrendResponse response = new HotTrendResponse(
                (List<HotwordDTO>) hotTrends.get(0), // day
                (List<HotwordDTO>) hotTrends.get(1), // week
                (List<HotwordDTO>) hotTrends.get(2)  // month
        );

        // 返回响应
        return CommonResponse.createForSuccess(response);
    }

    //记录存储
    public void handleSearchRequest(String keyword, Integer userId) {
        // 调用接口并收集数据
        KeywordDTO keywordInfo = keywordService.getKeywordInfo(keyword);
        Integer rate = keywordService.getKeywordRate(keyword);
        List<KeywordDTO> midWords = keywordService.getLongTailWords(keyword);
        List<CompKey> competitiveWords = keywordService.getCompetitiveWords(keyword);
        List<Word> trendData = keywordService.getKeywordTrend(keyword);

        // 调用insertKeywordRecord方法保存数据
        keywordRecordService.insertKeywordRecord(keyword, midWords, competitiveWords, trendData, rate, userId);
    }
}

